home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 42 / Amiga Format AFCD42 (Issue 126, Aug 1999).iso / -serious- / programming / other / ioblixdevkit / c / examples / queryparports.c < prev    next >
C/C++ Source or Header  |  1999-05-14  |  6KB  |  119 lines

  1. /*
  2.     This program is meant to demonstrate how to get all necessary information
  3.     about a specific chip on an IOBlix board and how to program that chip in a
  4.     system-friendly manner
  5. */
  6.  
  7. #include <exec/exec.h>
  8. #include <resources/ioblix.h>
  9. #include <ioblix/parport.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12.  
  13. #include <proto/exec.h>
  14. #include <proto/ioblix.h>
  15.  
  16.  
  17. UBYTE __aligned myname[]="QueryParPorts 37.2 "__AMIGADATE__;
  18. UBYTE __aligned version[]="$VER: QueryParPorts 37.2 "__AMIGADATE__;
  19. UBYTE __aligned copyright[]="(C)opyright 1998,1999 by Thore Böckelmann and RBM Computertechnik. All rights reserved";
  20.  
  21. struct IOBlixResource *IOBlixBase = NULL;
  22.  
  23. void main(void)
  24. {
  25.     struct IOBlixChipNode *chipNode;
  26.     ULONG board;
  27.     ULONG unit;
  28.     UBYTE *prevOwner;
  29.     struct ParPortRegisters *pp;
  30.     BOOL foundSomething;
  31.  
  32.     IOBlixBase = (struct IOBlixResource *)OpenResource(IOBLIXRESNAME);
  33.     if (IOBlixBase) {
  34.         for (board = 0; board < IOBLIX_MAX_Z2_BOARDS; board++) {
  35.             printf("board %ld\n", board);
  36.             foundSomething = FALSE;
  37.             for (unit = 0; unit < IOBLIX_Z2_NUM_PARUNITS; unit++) {
  38.                 /* first simply try to find each parallel port    */
  39.                 /* if it is available then print some information */
  40.                 chipNode = FindChip(ICT_Z2_PARALLEL_CHIP, board*10+unit);
  41.                 if (chipNode) {
  42.                     foundSomething = TRUE;
  43.                     printf("\tparallel port %ld is a \"%s\" with %ld bytes FIFO\n", unit, chipNode->icn_Description, chipNode->icnp_FIFOSize);
  44.                     printf("\tcurrent user: %s\n", (chipNode->icn_Owner) ? chipNode->icn_Owner : (UBYTE *)"nobody");
  45.                     printf("\tabilities: ");
  46.                     if (chipNode->icnp_Abilities & PCPAF_SPP) printf("SPP ");
  47.                     if (chipNode->icnp_Abilities & PCPAF_PPF) printf("PPF ");
  48.                     if (chipNode->icnp_Abilities & PCPAF_PS2) printf("PS2 ");
  49.                     if (chipNode->icnp_Abilities & PCPAF_EPP) printf("EPP ");
  50.                     if (chipNode->icnp_Abilities & PCPAF_ECP) printf("ECP ");
  51.                     printf("\n");
  52.  
  53.                     /* now try to obtain chip for exclusive use */
  54.                     prevOwner = NULL;
  55.                     chipNode = ObtainChip(ICT_Z2_PARALLEL_CHIP, board*10+unit, myname, &prevOwner);
  56.                     if (chipNode) {
  57.                         /* for this test we need SPP mode and the ability to switch to other modes with */
  58.                         /* the ECR register */
  59.                         if ((chipNode->icnp_Abilities & (PCPAF_SPP | PCPAF_ECR)) == (PCPAF_SPP | PCPAF_ECR)) {
  60.                             UBYTE oecr;
  61.                             UBYTE testString[] = "The quick brown fox jumps over the lazy dog";
  62.                             UBYTE readBack[256];
  63.                             ULONG cnt, writeCnt;
  64.  
  65.                             pp = (struct ParPortRegisters *)chipNode->icn_ChipRegisters;
  66.                             /* now play a bit with the parport's test mode */
  67.                             /* every value written to the FIFO should also be read back */
  68.                             /* else something really bad has happened */
  69.  
  70.                             oecr = *pp->pr_econtrol;
  71.                             /* first set port in SPP mode, else TST mode cannot be activated */
  72.                             /* turn off parport interrupts */
  73.                             *pp->pr_econtrol = PARPORT_ECONTROL_SPP | PARPORT_ECONTROL_INT;
  74.                             *pp->pr_econtrol = PARPORT_ECONTROL_TST | PARPORT_ECONTROL_INT;
  75.  
  76.                             /* write some data to the FIFO */
  77.                             cnt = 0;
  78.                             do {
  79.                                 *pp->pr_tfifo = testString[cnt];
  80.                                 cnt++;
  81.                             } while ((testString[cnt]) && !(*pp->pr_econtrol & PARPORT_ECONTROL_FIFO_F));
  82.                             writeCnt = cnt;
  83.                             printf("\t\twrote %ld bytes of \"%s\" to FIFO\n", writeCnt, testString);
  84.  
  85.                             /* and now read the data back */
  86.                             cnt = 0;
  87.                             do {
  88.                                 readBack[cnt] = *pp->pr_tfifo;
  89.                                 cnt++;
  90.                             } while ((cnt < writeCnt));
  91.                             readBack[cnt] = 0x00;
  92.                             printf("\t\tread %ld bytes back from FIFO. \"%s\"\n", cnt, readBack);
  93.  
  94.                             /* restore old econtrol contents */
  95.                             *pp->pr_econtrol = PARPORT_ECONTROL_SPP | PARPORT_ECONTROL_INT;
  96.                             *pp->pr_econtrol = oecr;
  97.                         } else {
  98.                             printf("parallel port doesn't support anything else than SPP!\n");
  99.                         }
  100.  
  101.                         /* now release the chip again */
  102.                         ReleaseChip(chipNode);
  103.                     } else {
  104.                         /* ObtainChip() failed, so let's check for other owners */
  105.                         if (prevOwner) printf("chip is already in use by \"%s\"\n", prevOwner);
  106.                     }
  107.                 }
  108.             }
  109.             if (!foundSomething) {
  110.                 printf("\tnothing found for this board\n");
  111.             }
  112.             printf("\n");
  113.         }
  114.     } else {
  115.         printf("Can't find ioblix.resource. Please run SetupIOBlix\n");
  116.     }
  117. }
  118.  
  119.